package com.erp.gather.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.erp.gather.mapper.ErpCollectProductMapper;
import com.erp.gather.service.IErpAutoCollectProductTemporaryService;
import com.erp.gather.service.IErpAutoPublishProductProjectDataProcessService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.gather.api.domain.dto.AutoQueryInfoDto;
import com.ruoyi.gather.api.domain.dto.AutoQueryPriceTemplateInfoDto;
import com.ruoyi.shop.api.RemoteSellerStoreService;
import com.ruoyi.shop.api.domain.ErpSellerStoreEmpower;
import com.ruoyi.shop.api.domain.ErpSellerStorePriceTemplate;
import com.ruoyi.shop.api.domain.ErpSellerStorePromotionActivity;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class ErpAutoPublishProductProjectDataProcessServiceImpl implements IErpAutoPublishProductProjectDataProcessService {
    @Resource
    ErpCollectProductMapper erpCollectProductMapper;
    @Resource
    IErpAutoCollectProductTemporaryService iErpAutoCollectProductTemporaryService;
    @Resource
    RemoteSellerStoreService remoteSellerStoreService;

    @Override
    public R<Object> queryStoreProductNumberNew(AutoQueryInfoDto dto) {
        //店铺id
        Long storeId = dto.getStoreId();
        R<Object> storeR = remoteSellerStoreService.getSellerStoreInfoById(storeId);
        String storeString = JSONObject.toJSONString(storeR.getData());
        ErpSellerStoreEmpower store = JSONObject.parseObject(storeString, ErpSellerStoreEmpower.class);
        if (Objects.isNull(store)) {
            return R.fail("店铺不存在");
        }
        //用户id
        dto.setUserId(store.getUserId());
        dto.setStoreUserName(store.getStoreUserName());

        //从公共库/品牌库中取商品
        Integer pulishNumberByDay = dto.getPulishNumberByDay();
        List<String> collectProductList = new ArrayList<>();
        Integer listNumber = 0;
        if (ObjectUtils.isNull(pulishNumberByDay)) {
            //是空，则代表是前端页面调取
            //从其他表中取
            collectProductList = erpCollectProductMapper.selectCollectProductListForAuto(dto);
        } else {
            if (pulishNumberByDay - collectProductList.size() > 0) {
                //没取得所需要的条数
                collectProductList = erpCollectProductMapper.selectCollectProductListForAutoTemproary(dto);
                //将该类目的商品重新初始化注入
                iErpAutoCollectProductTemporaryService.initializeLibrary(dto.getProductCategory());
                collectProductList = erpCollectProductMapper.selectCollectProductListForAutoTemproary(dto);
                if (collectProductList.size() == 0) {
                    return R.fail(0);
                }
            }
        }

        return R.ok(collectProductList.size());
    }


    @Override
    public R<Object> queryAutoStoreProductListNew(AutoQueryInfoDto dto) {
        //店铺id
        Long storeId = dto.getStoreId();
        R<Object> storeR = remoteSellerStoreService.getSellerStoreInfoById(storeId);
        String storeString = JSONObject.toJSONString(storeR.getData());
        ErpSellerStoreEmpower store = JSONObject.parseObject(storeString, ErpSellerStoreEmpower.class);
        if (Objects.isNull(store)) {
            return R.fail("店铺不存在");
        }
        //0-公共库 1-个人库
        List<String> collectProductList = erpCollectProductMapper.selectCollectProductListForAutoTemproary(dto);
        //应取得条数
        Integer pulishNumberByDay = dto.getPulishNumberByDay();
        if (pulishNumberByDay - collectProductList.size() > 0) {
            //没取得所需要的条数
            //将该类目的商品重新初始化注入
            iErpAutoCollectProductTemporaryService.initializeLibrary(dto.getProductCategory());
            collectProductList = erpCollectProductMapper.selectCollectProductListForAutoTemproary(dto);
            if (collectProductList.size() == 0) {
                return R.fail(0);
            }
        }

        return R.ok(collectProductList);
    }

    @Override
    public R<Object> queryAutoStoreProductListByUnshelf(AutoQueryInfoDto dto) {
        //店铺id
        Long storeId = dto.getStoreId();
        R<Object> storeR = remoteSellerStoreService.getSellerStoreInfoById(storeId);
        String storeString = JSONObject.toJSONString(storeR.getData());
        ErpSellerStoreEmpower store = JSONObject.parseObject(storeString, ErpSellerStoreEmpower.class);
        if (Objects.isNull(store)) {
            return R.fail("店铺不存在");
        }
        //0-公共库 1-个人库
        List<String> collectProductList = erpCollectProductMapper.selectCollectProductListForAutoTemproaryByUnshelf(dto);
        //应取得条数
        Integer pulishNumberByDay = dto.getPulishNumberByDay();
        if (collectProductList.size() - pulishNumberByDay <= 0) {
            //没取得所需要的条数
            //将该类目的商品重新初始化注入
            iErpAutoCollectProductTemporaryService.initializeLibraryByUnshelf(dto.getProductCategory());
            collectProductList = erpCollectProductMapper.selectCollectProductListForAutoTemproaryByUnshelf(dto);
            if (collectProductList.size() == 0) {
                return R.fail(0);
            }
        }

        return R.ok(collectProductList);
    }

}
